package com.witte.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;

import com.witte.entity.RespBean;


//用户登录session过滤器
@WebFilter(filterName = "PerInfoFilter", urlPatterns = "/api/*")
public class PerInfoFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest HttpRequest = (HttpServletRequest) request;
		HttpServletResponse HttpResponse = (HttpServletResponse) response;
		if(HttpRequest.getSession()==null || HttpRequest.getSession().getAttribute("username") == null || HttpRequest.getSession().getAttribute("username").equals(""))
		{
			System.out.println("拦截");
			MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
			converter.write(RespBean.build(203, "未登录账号", null), RespBean.class, MediaType.APPLICATION_JSON_UTF8, new ServletServerHttpResponse(HttpResponse));
		}
		else {
			chain.doFilter(request, response);
		}
	}

}
